package com.parkreport.admin.cararea.service;

import java.util.List;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.pager.PageHelper;
import com.common.util.StringUtil;
import com.parkreport.vo.CarAreaVO;
import com.parkreport.vo.CarcolorVO;

public class CarAreaService extends BaseService {

	public List find(CarAreaVO vo,PageHelper pager){
		StringBuilder sql = new StringBuilder("select * from CAR_AREA where 1=1 ");
		if(vo!=null){
			if(!StringUtil.isBlank(vo.getCityname())){
				sql.append(" and CITYNAME like '%"+vo.getCityname()+"%'");
			}
			if(!StringUtil.isBlank(vo.getProvincename())){
				sql.append(" and PROVINCENAME like '%"+vo.getProvincename()+"%'");
			}
			if(!StringUtil.isBlank(vo.getCarnum())){
				sql.append(" and CARNUM like '%"+vo.getCarnum()+"%'");
			}
		}
		
		
		List<CarAreaVO> list = DB4ObjUtil.executePager4Obj(new CarAreaVO(), sql.toString(),pager);
		
		return list;
	}
	
	public CarAreaVO get(String id){
		String sql = "select * from CAR_AREA where 1=1 and CarAreaID=?";
		List<CarAreaVO> list = DB4ObjUtil.execute4Obj(new CarAreaVO(), sql, id);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}
	

	public void delete(String ids){
		String sql = "delete from CAR_AREA where CarAreaID=?";
		
		if(!StringUtil.isBlank(ids)){
			String[] idArr = ids.split(",");
			DBBaseUtil.executeBatchUpdate(sql, idArr);
			
		}
	}
	
	public void delete(String[] ids){
		String sql = "delete from CAR_AREA where CarAreaID=?";
		
		if(!StringUtil.isBlank(ids)){
			DBBaseUtil.executeBatchUpdate(sql, ids);
			
		}
		
		
	}
	public String save(CarAreaVO vo){
		if(vo!=null){
			String isHave = isApply(vo);
			if("0".equals(isHave)){
				if(StringUtil.isBlank(vo.getCarareaid())){
						String id = StringUtil.getUuid();
						String sql = "insert into CAR_AREA(carNum,ProvinceName,CityName) values(?,?,?)";
						DBBaseUtil.executeUpdate(sql, vo.getCarnum(),vo.getProvincename(),vo.getCityname());
				}else{
					
					String sql = "update CAR_AREA set carNum=?,ProvinceName=?,CityName=?  where CarAreaID=?";
					DBBaseUtil.executeUpdate(sql, vo.getCarnum(),vo.getProvincename(),vo.getCityname(),vo.getCarareaid());
				}
			}else{
				return "0";
			}
		}
		return "1";
	}
	
	public String isApply(String carNum,String id){
		String sql = "select * from CAR_AREA where 1=1 and carNum=? and CarAreaID<>?";
		List<CarAreaVO> list = DB4ObjUtil.execute4Obj(new CarAreaVO(), sql, carNum,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(CarAreaVO vo){
		String sql = "select * from CAR_AREA where 1=1 and carNum=? and CarAreaID<>?";
		List<CarAreaVO> list = DB4ObjUtil.execute4Obj(new CarAreaVO(), sql, vo.getCarnum(),vo.getCarareaid());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
}
